﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Lolitemchanger
{
    class CGestionFichierBinaire
    {
        StringBuilder strBinaryFile = null;
        byte[] BinaryFile = null;
        string m_strFileName = null;
        string m_strOldItemString = null;
        int StartIndex = 0;
        List<string> m_strlistitem = new List<string>();
        bool m_bItemReady = false;
        bool m_bWriteRequired = false;

        public bool IsItemReady()
        {
            return m_bItemReady;
        }

        public void Reset()
        {
            m_bItemReady = false;
            m_strlistitem.Clear();
            m_strOldItemString = null;
            BinaryFile = null;
            strBinaryFile = null;
            m_bWriteRequired = false;
        }
        public void SetFileName(string filename)
        {
            m_bItemReady = false;
            m_strOldItemString = null;
            BinaryFile = null;
            strBinaryFile = null;
            m_bWriteRequired = false;
            m_strFileName = filename;
        }
        public string GetFileName()
        {
            return m_strFileName;
        }
        public void ReadFile()
        {
            if (m_strFileName != null)
            {
                BinaryReader br = null;
                FileStream fs = null;
                byte tmp = new byte();
                try
                {

                    fs = File.Open(m_strFileName, FileMode.Open);
                    br = new BinaryReader(fs);
                    strBinaryFile = new StringBuilder();
                    BinaryFile = new byte[fs.Length+1];

                    while (fs.Position < fs.Length)
                    {
                        tmp = br.ReadByte();
                        strBinaryFile.Append((char)tmp);
                        BinaryFile[fs.Position-1] = tmp;
                    }

                 

                    if (br != null) br.Close();
                    Regex reg = new Regex("[0-9]{4}.[0-9]{4}.[0-9]{4}.[0-9]{4}.[0-9]{4}.[0-9]{4}");
                    string m_strOldItemString = reg.Match(strBinaryFile.ToString()).Value;
                    StartIndex = strBinaryFile.ToString().IndexOf(m_strOldItemString);
                    m_strlistitem.Clear();
                    m_strlistitem.Add(m_strOldItemString.Substring(0, 4));
                    m_strlistitem.Add(m_strOldItemString.Substring(5, 4));
                    m_strlistitem.Add(m_strOldItemString.Substring(10, 4));
                    m_strlistitem.Add(m_strOldItemString.Substring(15, 4));
                    m_strlistitem.Add(m_strOldItemString.Substring(20, 4));
                    m_strlistitem.Add(m_strOldItemString.Substring(25, 4));
                    m_bItemReady = true;



                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                }
            }
        }
        public void WriteFile()
        {
            if (m_strlistitem.Count == 6 && m_strFileName != null && BinaryFile != null)
            {

                BinaryWriter bw = null;
                FileStream fs = null;
                try
                {
                    int j = 0;
                    bw = new BinaryWriter(File.Create(m_strFileName));
                    for (int i = 0; i < strBinaryFile.Length; i++)
                    {

                        if (i >= StartIndex && i <= StartIndex + 28)
                        {
                            if (i >= StartIndex && i <= StartIndex + 3)
                            {
                                bw.Write(Convert.ToByte(((string)m_strlistitem[0])[j]));
                                j++;

                                if (j == 4)
                                    j = 0;
                            }
                            else if (i >= StartIndex + 5 && i <= StartIndex + 8)
                            {
                                bw.Write(Convert.ToByte(((string)m_strlistitem[1])[j]));
                                j++;

                                if (j == 4)
                                    j = 0;
                            }
                            else if (i >= StartIndex + 10 && i <= StartIndex + 13)
                            {
                                bw.Write(Convert.ToByte(((string)m_strlistitem[2])[j]));
                                j++;

                                if (j == 4)
                                    j = 0;
                            }
                            else if (i >= StartIndex + 15 && i <= StartIndex + 18)
                            {
                                bw.Write(Convert.ToByte(((string)m_strlistitem[3])[j]));
                                j++;

                                if (j == 4)
                                    j = 0;
                            }
                            else if (i >= StartIndex + 20 && i <= StartIndex + 23)
                            {
                                bw.Write(Convert.ToByte(((string)m_strlistitem[4])[j]));
                                j++;

                                if (j == 4)
                                    j = 0;
                            }
                            else if (i >= StartIndex + 25 && i <= StartIndex + 28)
                            {
                                bw.Write(Convert.ToByte(((string)m_strlistitem[5])[j]));
                                j++;

                                if (j == 4)
                                    j = 0;
                            }
                            else
                            {
                                bw.Write((byte)BinaryFile[i]);
                            }

                        }
                        else
                        {
                            bw.Write((byte)BinaryFile[i]);
                        }
                    }
                    m_bWriteRequired = false;
                    bw.Close();

                }
                catch (Exception ex)
                {

                    if (bw != null) bw.Close();
                    System.Windows.MessageBox.Show(ex.Message);
                }
            }
        }
        public bool WriteRequired()
        {
            return m_bWriteRequired;
        }
        public void EditItem(int index,string itemnumber)
        {
            try
            {
                if (itemnumber.Length != 4)
                {
                    throw new Exception("numero d'item incorrect");
                }

                for (int i = 0; i < itemnumber.Length; i++)
                {
                    if(!char.IsNumber(itemnumber[i]))
                    {
                        throw new Exception("numero d'item incorrect");
                    }
                    
                }

                m_strlistitem[index] = itemnumber;
                m_bWriteRequired = true;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                
            }

        }
        public string GetItemNumber(int index)
        {
            return (string)m_strlistitem[index];
        }
    }
}
